<?php
include_once 'campo.inc.php';
include_once 'db.inc.php';

class Report{
    public $titulo;
    public $campos=array();//array(nombre=>titulo)
    public $tipos=array(); //array(nombre=>tipo)
    public $tabla=null;
    public $filtros=array();//array(new FiltroCampo,...)
    public $dbManager; // new DbManager
    public $dataTable;
    public $filtroFijo=array();
    public $sql=null;
    public $sessionName="reporte";
    public $ajax=false;
    public $requireFilter=true;
    public $final;
    public $preLoaded=false;
    public $sumArea=false;
    public $sumFields=array();

    function ready(){
        header("Location: report.php");
    }

    function buildFields(){
        $fields="<tr>";
        foreach($this->filtros as $campo){
            $fields.="<td>";
            $fields.=$campo->etiqueta;
            $fields.="</td>";
            $fields.="<td>";
            $fields.=$this->buildField($campo->nombre,"text",$campo->long);
            $fields.="</td>";
        }
        $fields.="</tr>";
        return $fields;
    }

    function buildField($name,$type,$size="50"){
        return "<input name='$name' id='$name' type='$type' size='$size'/>";
    }

    function load(){
        $w="";
        if(!$this->requireFilter){
            $w=$this->staticWhere();
        }else{
            $w=$this->getWhere();
        }
        $this->final=$w;
        if($this->tabla!=null){
            $this->dataTable=$this->dbManager->query_select($this->tabla,$w);
        }
        if($this->sql!=null){
            if($w!=""){
                $w=" WHERE ".$w;
            }
            $this->dataTable=$this->dbManager->queryToArray($this->sql.$w);
        }
        // echo $w;
    }

    function getWhere($fromPage=false){
        $where="";
        $i=0;
        foreach($this->filtros as $campo){
            $valor=$campo->defValue==NULL?$this->getParam($campo->nombre):$campo->defValue;
            if($valor!=NULL){
                $where .= $i>0?" AND ":"";
                $where .= $campo->nombre."=".$this->sqlValue($valor, $campo->tipo);
                $i++;
            }
        }

        $where.=$this->staticWhere();
        return $where;
    }

    function staticWhere(){
        $i=0;
        foreach($this->filtroFijo as $campo){
            $where .= $i>0?" AND ":"";
            if($campo->specialCriteria!=null){
                $where.=$campo->specialCriteria;
            }else{
                $valor=($campo->defValue!=null?$campo->defValue:$this->getParam($campo->nombre));
                $where .= $campo->nombre."=".$this->sqlValue($valor, $campo->tipo);
            }
            $i++;
        }
        return $where;
    }

    function buildTable($tableClass,$rowClass1,$rowClass2){
        $html="<table class='$tableClass' cellpadding='0' cellspacing='1' width='100%'>";
        $titles="<thead><tr>";
        foreach($this->campos as $nombre=>$titulo){
            $titles.="<th>$titulo</th>";
        }
        $titles.="</tr></thead>";
        $html.=$titles."<tbody>";
        $index=0;
        $rows="";
        if(count($this->dataTable)>0){
            foreach($this->dataTable as $row){
                $rowClass=fmod($index,2)==0?$rowClass2:$rowClass1;
                $rows.="<tr class='$rowClass'>";
                foreach($this->campos as $nombre=>$titulo){
                    $rows.="<td>";
                    $rows.=htmlentities(htmlspecialchars($this->formattVal($row[$nombre],$this->tipos[$nombre])));
                    $rows.="</td>";
                }
                $rows.="</tr>";
                $index++;
            }
        }else{
            $rows.="<tr class='$rowClass1'>";
            foreach($this->campos as $nombre=>$titulo){
                $rows.="<td>&nbsp;";
                $rows.="</td>";
            }
            $rows.="</tr>";
        }
        $html.=$rows."<tbody></table>";
        if($this->sumArea){
            $html.="&nbsp;".$this->buildSumArea($tableClass);
        }
        return $html;
    }

    function formattVal($val,$tipo){
        switch($tipo){
            case "int": return intval($val);
                break;
            case "text":return $val;
                break;
            case "num":return number_format($val,2);
                break;
        }
        return $val;
    }

    function buildSumArea($tableClass){
        $html="<table class='$tableClass' cellpadding='0' cellspacing='1'><tbody>";
        $titles="<tr>";
        $sumas="<tr>";
        $titles.="<th>Total Registros</th>";
        $total=count($this->dataTable);
        $sumas.="<td>$total<dtr>";
        $total=0;
        foreach($this->sumFields as $titulo=>$campo){
            $titles.="<th>$titulo</th>";
            $suma=0;
            foreach($this->dataTable as $row){
                $suma+=$row[$campo];
            }
            $suma=number_format($suma, 2);
            $sumas.="<td>$suma</td>";
        }

        $sumas.="</tr>";
        $titles.="</tr>";

        $html.=$titles.$sumas;

        return $html;
    }

    function getParam($name, $default = null)
    {
        if (array_key_exists($name, $_REQUEST)) {
            $param = $_REQUEST[$name];
            if ($default != null && isEmpty($param)) {
                return $default;
            }
            return $param;
        } else
        return $default;
    }

    function sqlValue($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
    {
        $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

        $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

        switch ($theType) {
            case "text":
                $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
                break;
            case "long":
                case "int":
                    $theValue = ($theValue != "") ? intval($theValue) : "NULL";
                    break;
                case "double":
                    $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
                    break;
                case "date":
                    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
                    break;
                case "defined":
                    $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
                    break;
            }
            return $theValue;
        }

    }
    ?>
